<template>
	<view class="u-wrap">
		<view class="u-search-box">
			<view class="sousuo">
				<u-search :disabled="true" @click="gopage" @custom='gopage' v-model="sousuoVal" placeholder="请输商品名称" height="48"
				 margin="16rpx 0" :action-style="sousouBtn" bg-color="#fff"></u-search>
			</view>
		</view>
		<view class="u-menu-wrap">
			<scroll-view scroll-y scroll-with-animation class="u-tab-view menu-scroll-view" :scroll-top="scrollTop">
				<!-- <view v-for="(item,index) in tabbar" :key="index" class="u-tab-item" :class="[current==index ? 'u-tab-item-active' : '']"
				 :data-current="index" @tap.stop="swichMenu(index)">
					<text class="u-line-1">{{item.cate_name}}</text>
				</view> -->
				<uni-collapse accordion="true" @change="tabsChange" v-if="reloadBoole">
					<uni-collapse-Item v-for="(item,index) in tabbar" :key='index' :title="item.cate_name" :open="item.cate_name == openTanbs ? true : false ">
						<view style="padding: 30rpx; color: red;" v-for="(item1,index1) in item.child" :class="[index1  == pinpaixz ? '' :'pinpaiCss']"
						 @click="getShoparr(item,item1,index1)">
							{{item1.cate_name}}
						</view>
					</uni-collapse-Item>

				</uni-collapse>
			</scroll-view>
			<block v-for="(item,index) in tabbar" :key="index">
				<scroll-view scroll-y class="right-box" v-if="current==index" @scrolltolower="nexPage">
					<view class="page-view">
						<view class="class-item">
							<!-- 	<view class="item-title">
								<text>{{item.name}}</text>
							</view> -->
							<view class="item-container">
								<u-row>
									<u-col span="6" v-for="(item1,index1) in shoparr" :key='index1'>
										<view class="shopval" @click="goxiangqing(item1)">
											<u-image width="100%" height="344rpx" :src="item1.image" mode="aspectFit" :fade="false"></u-image>
											<view class="shop_name">
												{{item1.store_name}}
											</view>
											<view class="shop_msg">
												{{item1.store_name}}
											</view>
											<view class="jiageBox">
												FCFA{{item1.price}}
											</view>
										</view>
									</u-col>
								</u-row>
							</view>
						</view>
					</view>
				</scroll-view>
			</block>
		</view>
	</view>
</template>

<script>
	import uniCollapse from '@/components/uni-collapse/uni-collapse.vue'
	import uniCollapseItem from '@/components/uni-collapse-item/uni-collapse-item.vue'
	export default {
		components: {
			uniCollapse,
			uniCollapseItem
		},
		data() {
			return {
				tabbar: [],
				scrollTop: 0, //tab标题的滚动条位置
				current: 0, // 预设当前项的值
				menuHeight: 0, // 左边菜单的高度
				menuItemHeight: 0, // 左边菜单item的高度
				shoparr: [],
				sousouBtn: {
					width: "100rpx",
					background: "#808080",
					borderRadius: '24rpx',
					color: "#fff",
					fontSize: "26rpx",
					height: "48rpx",
					lineHeight: "48rpx"
				},
				info: null,
				itemId: null,
				page: 1,
				pinpai: [{
					name: "无",
				}],
				openTanbs: '',
				pinpaixz: 0,
				item: null,
				reloadBoole: true
			}
		},
		computed: {

		},
		onLoad() {
			// uni.showShareMenu()
			uni.getStorage({
				key: 'userinfo',
				success: (res) => {
					this.info = res.data
				}
			})
			this.getcaidan()
		},
		onShow() {
			this.showFun()
			this.reloadBoole = true
			this.getcaidan()
		},
		onReachBottom() {
			this.page++
			this.getlist()
			console.log('555');
		},
		onHide() {
			getApp().globalData.index = 0
			this.openTanbs = ''
			this.tabbar = []
			this.reloadBoole = false
			this.page = 1
		},
		methods: {
			showFun() {
				this.pinpaixz = 0
				this.$nextTick(function() {
					var index = getApp().globalData.index
					this.openTanbs = index;
					if (!index) {
						this.openTanbs = this.tabbar[0].cate_name
						console.log(this.openTanbs);
					}
					this.item = this.tabbar[0]
					console.log(getApp().globalData);
					for (var i = 0; i < this.tabbar.length; i++) {
						if (this.tabbar[i].cate_name == index) {
							this.item = this.tabbar[i]
						}
					}
					this.getlist()

				})
			},
			getcaidan() {
				uni.request({
					url: this.$AJAX_URL + 'goods/category',
					method: 'POST',
					success: (res) => {
					
		console.log('所有类型',res.data.data);
						this.tabbar = res.data.data
						this.itemId = this.tabbar[0].id
						this.item = this.tabbar[0]
						this.getlist()
						this.showFun()
					}
				})
			},
			nexPage() {
				this.page++
				var sid = ''
				if (this.item.child[this.pinpaixz]) {
					sid = this.item.child[this.pinpaixz].id;
				}
				uni.request({
					url: this.$AJAX_URL + 'goods/product_list',
					method: 'POST',
					data: {
						page: this.page,
						cid: this.item.id,
						sid: sid
					},
					success: (res) => {
						this.shoparr.push(...res.data.data.list)
					}
				})
			},
			getlist() {
				var sid = ''
				console.log(this.item);
				if (this.item.child[this.pinpaixz]) {
					sid = this.item.child[this.pinpaixz].id;
				}
				uni.request({
					url: this.$AJAX_URL + 'goods/product_list',
					method: 'POST',
					data: {
						page: this.page,
						cid: this.item.id,
						sid: sid
					},
					success: (res) => {
						console.log('商品列表',res);
						this.shoparr = res.data.data.list
					}
				})
			},
			gopage() {
				uni.navigateTo({
					url: '../sousuo/sousuo'
				})
			},
			getImg() {
				return Math.floor(Math.random() * 35);
			},
			// 点击左边的栏目切换
			// async swichMenu(index) {
			// 	if (index == this.current) return;
			// 	this.page = 1
			// 	this.current = index;
			// 	this.itemId = this.tabbar[index].id
			// 	this.shoparr = []
			// 	this.getlist()
			// 	// 如果为0，意味着尚未初始化
			// 	if (this.menuHeight == 0 || this.menuItemHeight == 0) {
			// 		await this.getElRect('menu-scroll-view', 'menuHeight');
			// 		await this.getElRect('u-tab-item', 'menuItemHeight');
			// 	}
			// 	// 将菜单菜单活动item垂直居中
			// 	this.scrollTop = index * this.menuItemHeight + this.menuItemHeight / 2 - this.menuHeight / 2;
			// },
			// 获取一个目标元素的高度
			// getElRect(elClass, dataVal) {
			// 	new Promise((resolve, reject) => {
			// 		const query = uni.createSelectorQuery().in(this);
			// 		query.select('.' + elClass).fields({
			// 			size: true
			// 		}, res => {
			// 			// 如果节点尚未生成，res值为null，循环调用执行
			// 			if (!res) {
			// 				setTimeout(() => {
			// 					this.getElRect(elClass);
			// 				}, 10);
			// 				return;
			// 			}
			// 			this[dataVal] = res.height;
			// 		}).exec();
			// 	})
			// },
			goxiangqing(item) {
				uni.navigateTo({
					url: '../xiangqing/xiangqing?id=' + item.id
				})
			},
			tabsChange(val) {
				console.log(val);
				console.log('ssssss');
				this.page = 1
				if (val[0] || val[0] == 0) {
					this.pinpaixz = 0;
					this.item = this.tabbar[val[0]]
					this.getlist()
				} else {
					console.log(false);
				}
			},
			getShoparr(item, item1, index1) {
				this.page = 1
				console.log(item);
				console.log(item1);
				console.log(index1);
				this.pinpaixz = index1
				this.getlist()
			}
		}
	}
</script>

<style lang="scss" scoped>
	page {
		background-color: #f7f7f7;
	}

	.u-wrap {
		height: calc(100vh);
		/* #ifdef H5 */
		height: calc(100vh - var(--window-top));
		/* #endif */
		display: flex;
		flex-direction: column;
	}

	// .u-search-box {
	// 	padding: 18rpx 30rpx;
	// }

	.u-menu-wrap {
		flex: 1;
		display: flex;
		overflow: hidden;
	}

	page {
		background-color: rgb(234, 234, 234);
	}

	.u-search-inner {
		background-color: rgb(234, 234, 234);
		border-radius: 100rpx;
		display: flex;
		align-items: center;
		padding: 10rpx 16rpx;
	}

	.u-search-text {
		font-size: 26rpx;
		color: $u-tips-color;
		margin-left: 10rpx;
	}

	.u-tab-view {
		width: 220rpx;
		height: 100%;
	}

	.u-tab-item {
		height: 110rpx;
		background: #f6f6f6;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 26rpx;
		color: #444;
		font-weight: 400;
		line-height: 1;
		padding-left: 20rpx;
	}

	.u-tab-item-active {
		position: relative;
		color: #000;
		// font-size: 30rpx;
		// font-weight: 600;
		background: #fff;
	}

	.u-tab-item-active::before {
		content: "";
		position: absolute;
		border-left: 4px solid #000;
		height: 32rpx;
		left: 0;
		top: 39rpx;
	}

	.u-tab-view {
		height: 100%;
	}

	.right-box {
		background-color: rgb(250, 250, 250);
	}

	.page-view {
		padding: 16rpx;
	}

	.class-item {
		margin-bottom: 30rpx;
		background-color: #fff;
		padding: 16rpx;
		border-radius: 8rpx;
	}

	.item-title {
		font-size: 26rpx;
		color: $u-main-color;
		font-weight: bold;
	}

	.item-menu-name {
		font-weight: normal;
		font-size: 24rpx;
		color: $u-main-color;
	}

	.item-container {
		// display: flex;
		flex-wrap: wrap;
	}

	.thumb-box {
		width: 33.333333%;
		display: flex;
		align-items: center;
		justify-content: center;
		flex-direction: column;
		margin-top: 20rpx;
	}

	.item-menu-image {
		width: 120rpx;
		height: 120rpx;
	}

	.shopval {
		margin-bottom: 20rpx;
		height: 530rpx;
		border-radius: 20rpx;
		background: #FFFFFF;
		overflow: hidden;

		.shop_name {
			margin-left: 15rpx;
			font-size: 32rpx;
			margin-top: 19rpx;
			margin-right: 20rpx;
			overflow: hidden;
			text-overflow: ellipsis;
			white-space: nowrap;
		}

		.shop_msg {
			margin-left: 10rpx;
			margin-right: 28rpx;
			font-size: 24rpx;
			color: #333333;
			margin-top: 10rpx;
			overflow: hidden;
			text-overflow: ellipsis;
			white-space: nowrap;
		}

		.jiageBox {
			color: #FF392B;
			font-size: 30rpx;
			margin-left: 17rpx;
			margin-top: 10rpx;
		}
	}

	.sousuo {
		padding: 0 30rpx;
		// background: #FFFFFF;
	}

	.pinpaiCss {
		background: #FFFFFF;
		color: #333333 !important;
	}
</style>
